package com.sanpu.kxe.common.view;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.utils.animations.AnimationForViewUtil;
import com.sanpu.kxe.utils.animations.RotationParams;

/**
 * Description：旋转加载Loading
 * Created by 薛勇军 on 2018/6/11.
 */

public class IssLoadingDialog extends Dialog {

    private Context mContext;

    private TextView loadingText;

    private MoveRoundView loading_progress;

    private String def_txt_show;

    private ObjectAnimator objectAnimator;

    public IssLoadingDialog(Context context) {
        super(context, R.style.dialog_normal);
        mContext = context;
        setContentView(R.layout.loading);
        setProperty();
        setCancelable(false);
        loadingText = (TextView) findViewById(R.id.loading_text);
        loading_progress = (MoveRoundView) findViewById(R.id.loading_progress);
        def_txt_show = loadingText.getText().toString();
    }

    public void setLoadingMessage(String message) {
        if (!TextUtils.isEmpty(message)) {
            loadingText.setText(message);
        }
    }

    public void show() {
        super.show();
        // loading_progress.start();
        RotationParams rotationParams = new RotationParams(loading_progress);
        rotationParams.setDuration(2000);
        rotationParams.setStartDegrees(0);
        rotationParams.setEndDegrees(360);
        rotationParams.setRepeatCount(-1);
        objectAnimator = AnimationForViewUtil.rotationAsy(rotationParams);
    }

    public void show(String message) {
        super.show();
        if (!TextUtils.isEmpty(message)) {
            loadingText.setText(message);
        } else {
            reset();
        }
        show();
    }

    private void reset() {
        loadingText.setText(def_txt_show);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        //防止内存泄漏
        clear();

    }

    public void cancel() {
        super.cancel();
        clear();
    }

    private void clear() {
        loading_progress.stop();
        if (objectAnimator != null) {
            if (Looper.myLooper() == null) {
                loading_progress.post(new Runnable() {
                    @Override
                    public void run() {
                        objectAnimator.cancel();
                    }
                });
                return;
            }
            objectAnimator.cancel();
        }
    }

    private void setProperty() {
        Window window = getWindow();
        WindowManager.LayoutParams p = window.getAttributes();
        Display d = getWindow().getWindowManager().getDefaultDisplay();

        p.height = d.getHeight() * 1;
        p.width = d.getWidth() * 1;
        window.setAttributes(p);
    }
}
